<?php

namespace app\api\command;
use app\common\model\Packages;
use app\common\model\UserPackagesConfig;
use fast\Random;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Kgjorder extends Command
{
    protected $model = null;

    protected function configure()
    {
        $this->setName('kgjtongbu')->setHelp('卡管家订单信息同步')->setDescription('卡管家订单信息同步');

    }

    protected function execute(Input $input, Output $output)
    {
        $orderModel = new \app\common\model\Order();
        $order =  $orderModel->where('platform_type',6)->where('is_up',1)->where('is_agency',1)->whereIn('order_status','-1,1,2,3,4')->order('id desc')->select();
        if($order){
            $path = '/api/client/open/queryOrder';
            $key  = config('site.kgj_appkey');
            foreach ($order as $k=>$v){
                sleep(2);
                $packageInfo = Packages::where(['id'=>$v['goods_id']])->find();
                $data['outTradeNo'] = $v['order_id'];
                $res = kgjcurl($path, $data,$key);
                $arr = json_decode($res,true);
                dump($arr);
//                unset($data);
//                if(!empty($arr['rows'][0])){
//                    $rows = $arr['rows'][0];
//                    $v->order_status  = $rows['order_status'];
//                    if(strpos($rows['remark'],'已激活') !== false){
//                        $v->order_status = 1;
//                    }
//
//                    if(strpos($rows['remark'],'重新提交') !== false){
//                        $v->order_status = 0;
//                    }
//                    if(strpos($rows['remark'],'100') !== false || strpos($rows['remark'],'可结算') ){
//                        $v->order_status = 5;
//                        $v->settlement_time = time();
//                        $userData = findAncestors($v->user_id);
//                        if(!$userData){
//                            $config = new UserPackagesConfig();
//                            $rebate = $config->where(['user_id'=>$v->user_id,'packages_id'=>$v->goods_id])->value('money');
//                            if($rebate){
//                                $packageInfo['rebate']  = $rebate;
//                            }
//                            //顶级代理下单
//                            dingji($v->user_id,$v->id,$v->goods_id,$packageInfo['rebate'],false,true);
//                        }else{
//                            array_unshift($userData,$v->user_id);
//                            $money = moneydigui($v->user_id,$v->goods_id);
//                            if($money){
//                                digui($v->user_id, $money,  $v->goods_id,  $v->id,0,true);
//                            }else{
//                                //顶级
//                                $config = new UserPackagesConfig();
//                                $rebate = $config->where(['user_id'=>$v->user_id,'packages_id'=>$v->goods_id])->value('money');
//                                if($rebate){
//                                    $packageInfo['rebate']  = $rebate;
//                                }
//                                dingji($userData[count($userData)-1],$v['id'],$v->goods_id,$packageInfo['rebate'],false,true);
//                            }
//                        }
//                    }
//                    if(!empty($v->admin_image)){
//                        $v->order_status = 1;
//                    }
//                    $v->order_info_json = json_encode($arr['rows'],true);
//                    $v->order_status_text = $rows['order_status_text'];
//                    $v->rebate            = $rows['rebate'];
//                    $v->channel           = $rows['channel'];
//                    $v->remark            = $rows['remark'];
//                    $v->package_name      = $packageInfo['name'];
//                    if(!empty($rows['phone']) && !$v->phone){
//                        $v->phone         = $rows['phone'];
//                    }
//                    if(!empty($rows['shipping_no'])){
//                        $v->shipping_no   = $rows['shipping_no'];
//                    }
//                    if(!empty($rows['shipping_company_name'])){
//                        $v->shipping_company_name = $rows['shipping_company_name'];
//                    }
//                    $v->is_recharged = $rows['is_recharged'];
//                    $r = $v->save();
//                }
//                if($r){
//                    echo "操作成功";
//                }else{
//                    echo "更新失败";
//                }
//                }

            }
        }

    }
}
